<?php
	namespace App\Services;

	use App\Models\Image     as mImage;
	use App\Trades\TradeBase as tTradeBase;

	class Image extends ServiceBase
	{
		public static function addNewImage($image_url, $target_type, $target_id = 0, $item_id = 0, $url = '')
		{
			$image_url = upload_qiniu($image_url);
			if (!$image_url) {
				return error('上传失败');
			}
			$image = new mImage;
			$image->create([
				'image_url'   => $image_url,
				'target_id'   => $target_id,
				'target_type' => $target_type,
				'item_id'     => $item_id,
				'url'         => $url,
			]);

			return $image;
		}

		public static function searchImages($cond = array(), $page = 0, $limit = 15)
		{
			$image = new mImage();

			$image = $image->search_images($cond);

			if(isset($cond['select'])){
				$image = $image->select($cond['select']);
			}
			if(isset($cond['target_type'])){
				$image = $image->where('target_type',$cond['target_type']);
			}
			if(isset($cond['item_id'])){
				$image = $image->where('item_id',$cond['item_id']);
			}
			if(isset($cond['created_at'])){
				$image = $image->where('created_at',$cond['created_at']);
			}
			if(isset($cond['orderBy'])){
				$image = $image->orderBy($cond['orderBy'],'desc');
			}
			if(isset($cond['nopage'])){
				return $image->get();
			}
			return $image->forPage($page,$limit)->get();

		}
		public static function updateImage($image,$data = [])
		{
			if(empty($image)){
				return error('image不存在');
			}
			if(count($data) == 0){
				return error('data不存在');
			}
			if(isset($data['image_url'])) {
				$data['image_url'] = upload_qiniu($data['image_url']);
				if (!$data['image_url']) {
					return error('上传失败');
				}
			}
			$image->update($data);

			return $image;
		}

		//
		public static function getImageByType($type, $id)
		{
			$image = new mImage;

			$image->where('target_type', $type)->where('target_id', $id)->first();
		}

		/**
		 * 获取项目所有进展图片
		 */
		public static function getItemProgressImages($item_id)
		{
			return (new mImage)->get_item_progress_images($item_id);
		}

		/**
		 * 获取进展图片
		 */
		public static function getProgressImages($progress_id)
		{
			return (new mImage)->get_progress_images($progress_id);
		}

		/**
		 * 获取横着的封面列表
		 */
		public static function getItemCoverImages($target_id)
		{
			return (new mImage)->get_item_cover_images($target_id);
		}
		/**
		 * 获取项目对应所有进展项目电影截图
		 */
		public static function getItemProgressScreenshotImages($item_id)
		{
			return (new mImage)->get_item_progress_screenshot_images($item_id);
		}

		/**
		 * 获取横着的封面
		 */
		public static function getItemCoverImage($item_id)
		{
			return (new mImage)->get_item_cover_image($item_id);
		}

		/**
		 * 获取竖着的封面
		 */
		public static function getItemLineCoverImage($id)
		{
			return (new mImage)->get_item_line_cover_image($id);
		}

		/**
		 * 获取竖着的封面列表
		 */
		public static function getItemLineCoverImages($id)
		{
			return (new mImage)->get_item_line_cover_images($id);
		}

		public static function getTypeByIndexBannerImages()
		{
			return (new mImage)->get_type_by_index_banner_images();
		}

		/*
		 * 获取首页广告轮播图列表
		 */
		public static function getIndexImagesList()
		{
			return (new mImage)->where('target_type', '=', mImage::TYPE_IMAGE_INDEX)->latest()->paginate(mImage::PAGE_BREAK);
		}

		/*
		 * 通过图片ID获取图片
		 */
		public static function getImageById($id)
		{
			return (new mImage)->find($id);
		}

		public static function getImageByTypeId($type, $item_id = null)
		{
			switch ($type) {
				case tTradeBase::TYPE_INCOME:
				case tTradeBase::TYPE_REFUND_BUY:
				case tTradeBase::TYPE_REFUND_INVEST:
				case tTradeBase::TYPE_REFUND_WITHDRAW:
					return '/img/ru11.png';
				case tTradeBase::TYPE_OUTCOME:
				case tTradeBase::TYPE_OUTCOME_BUY:
				case tTradeBase::TYPE_OUTCOME_WITHDRAW:
					return '/img/chu11.png';
				case tTradeBase::TYPE_INCOME_SHAREBUY:
					return '/img/fen11.png';
				case tTradeBase::TYPE_OUTCOME_INVEST:
				case tTradeBase::TYPE_INCOME_INVEST:
				default:
					return self::getItemLineCoverImage($item_id)->image_url;
			}
		}

		/**
		 * 通过项目ID获取团队成员背景图
		 */
		public static function getMemberBackgroundImageByItem($item_id)
		{
			return (new mImage)->get_member_background_image_by_item($item_id);
		}
	}
